# coding=utf-8

import time
import sys
sys.path.append("..")
from common.redis.connect import RedisConnector
from common.mysql.connect import MysqlConnector

#（已完成）
def init_goods_id_list():
    """ 商品数据，初始化需要处理的商品ID，实验：40977条数据耗时58s初始化完成
    """
    # return
    time_start = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    print("开始时间：" + time_start)

    mysql_db = MysqlConnector(5)
    redis_14 = RedisConnector(15)
    redis_14_pipe = redis_14.pipeline()

    sql = "SELECT id FROM `order` ORDER BY id ASC"
    res = mysql_db.select(sql)
    for goods_id in res:
        redis_14_pipe.lpush('goods_id_list', goods_id['id'])
    redis_14_pipe.execute()

    time_end = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    print("结束时间：" + time_end)


def init_user_id_list():
    """ 初始化需要处理的用户ID数据，实验：17058条数据耗时23s初始化完成
    """
    time_start = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    print("开始时间：" + time_start)

    mysql_db = MysqlConnector(6)
    redis_14 = RedisConnector(15)
    redis_14_pipe = redis_14.pipeline()

    sql = "SELECT id FROM `user` ORDER BY id ASC"
    res = mysql_db.select(sql)
    # 先清空之前的数据
    for user_id in res:
        redis_14_pipe.lpush('customer_id_list', user_id['id'])
    redis_14_pipe.execute()

    time_end = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    print("结束时间：" + time_end)


#（已完成）
def init_category_id_list():
    """ 商品分类数据统计
    """
    mysql_db = MysqlConnector()
    redis_14 = RedisConnector(14)

    sql = "SELECT id FROM ko_base_info WHERE lx_id = 11 ORDER BY id ASC"
    res = mysql_db.select(sql)
    for category_id in res:
        redis_14.lpush('goods_category_id_list', category_id['id'])
        # 同时，要查询分类下的商品ID，用于做后续的统计，每条分类ID都有自己的商品ID组成的长字符串
        sql_goods_id = "SELECT id FROM ko_stock WHERE kuan_id = %d \
            ORDER BY id ASC" % category_id['id']
        goods_ids = mysql_db.select(sql_goods_id)
        goods_id_init = '0'
        if goods_ids:
            for goods_id in goods_ids:
                goods_id_init += ',' + str(goods_id['id'])
        redis_14.set('goods_category_' + str(category_id['id']), goods_id_init)


#（已完成）
def init_assistant_id_list():
    """ 助理数据，初始化需要处理的助理ID
    """
    # print("assistant")
    mysql_db = MysqlConnector()
    redis_14 = RedisConnector(14)

    sql = "SELECT id FROM ko_manager WHERE zl_id > 0 ORDER BY id ASC"
    res = mysql_db.select(sql)
    for assistant_id in res:
        redis_14.lpush('assistant_id_list', assistant_id['id'])


#（已完成）
def init_department_id_list():
    """ 部门（甄选 + 珠宝）数据统计
    """
    # print("department")
    # mysql_db = MysqlConnector()
    redis_14 = RedisConnector(14)

    for department_id in range(1, 2):
        redis_14.lpush('department_id_list', department_id)


def init_salesman_id_list():
    """ 销售人数据统计
    """
    time_start = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    print("开始时间：" + time_start)

    mysql_db = MysqlConnector(6)
    redis_14 = RedisConnector(15)
    redis_14_pipe = redis_14.pipeline()

    sql = "SELECT id FROM `bojem_staff` ORDER BY id ASC"
    res = mysql_db.select(sql)
    # 先清空之前的数据
    for user_id in res:
        redis_14_pipe.lpush('customer_id_list', user_id['id'])
    redis_14_pipe.execute()

    time_end = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    print("结束时间：" + time_end)


if __name__ == '__main__':
    # 根据输入的参数进行初始化，调用格式：python redis_init.py assistant
    init_target = sys.argv[1]
    # print(init_target)

    if init_target == 'goods':
        # 商品数据
        init_goods_id_list()
    elif init_target == 'goods_category':
        # 商品分类数据
        init_category_id_list()
    elif init_target == 'assistant':
        # 助理号数据
        init_assistant_id_list()
    elif init_target == 'department':
        # 部门数据
        init_department_id_list()
    elif init_target == 'salesman':
        # 销售人员数据
        init_salesman_id_list()
    elif init_target == 'customer':
        # 客户人员数据
        init_user_id_list()
